/*
 * @Descripttion: 
 * @version: 
 * @Author: wtzhang
 * @Date: 2021-07-20 15:06:31
 * @LastEditors: wtzhang
 * @LastEditTime: 2021-07-20 15:17:17
 */
import { onUnmounted, onMounted, ref } from 'vue'

export default function useMousePosition() {
  const x = ref(-1)
  const y = ref(-1)
  // 声明一个点击事件回调函数
  const clickHandler = (event: MouseEvent) => {
    x.value = event.pageX
    y.value = event.pageY
  }
  // 点击事件在页面加载完毕后，在进行操作
  // 页面加载完毕后的生命周期组合API
  onMounted(() => {
    window.addEventListener('click', clickHandler)
  })
  // 页面销毁之前的生命周期组合API
  onUnmounted(() => {
    window.removeEventListener('click', clickHandler)
  })
  return { x, y }
}

